package com.itheima.reggie.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.reggie.common.CustomException;
import com.itheima.reggie.mapper.CategoryMapper;
import com.itheima.reggie.pojo.Category;
import com.itheima.reggie.pojo.Dish;
import com.itheima.reggie.pojo.Setmeal;
import com.itheima.reggie.service.CategoryService;
import com.itheima.reggie.service.DishService;
import com.itheima.reggie.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements CategoryService {

    @Autowired   //注入DishService以便调用其查询方法
    private DishService dishService;
    @Autowired  //注入DishService以便调用其查询方法
    private SetmealService setmealService;

    @Override
    public void deleteById(Long id) {
        //1.查询是否跟dish菜品表有关联   Sql语句 : select count(?) from dish where CategoryId=#{id}
        Integer count1 = dishService.lambdaQuery() //调用条件判断方法
                                    .eq(Dish::getCategoryId, id)//判断是否一致
                                    .count();//统计数量
        //判断是否有关联
        if (count1>0){
            //大于0代表有关联
            throw new CustomException("和菜品有联系,不能删除");
        }
        //2.查询是否跟setmeal套餐表有关联 Sql语句 : select count(?) from dish where CategoryId=#{id}
        Integer count2 = setmealService.lambdaQuery() //调用条件判断方法
                                       .eq(Setmeal::getCategoryId, id)//判断是否一致
                                       .count(); //统计数量
        //判断是否有关联
        if (count2>0){
            //大于0代表有关联
            throw new CustomException("和套餐有联系,不能删除");
        }
        //3.如果没有关联就调用本类的删除方法
        this.removeById(id);
    }
}
